<?php

/**
 * 商圈模型
 */

namespace app\api\model;

use PDOStatement;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;
use think\Db;

class BusinessArea extends Model {

    /**
     * 购房需求-商圈
     * @param $params
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getA($params) {
        return self::where('city_id', 'in',$params['city_code'])->field('id,name')->select()->toArray();
    }

    /**
     * 定制选房->商圈数据
     * @param $params
     * @param $pageSize
     * @return bool
     */
    public function getBusinessList($params,$pageSize) {
        $where=[];
        if ($params['area_code']!=$params['city_code'].'_all'){
            //选择区域传入
            $codes=explode(',',$params['area_code']);
            if (count($codes)>5){
                Err::add('区域选择不能超过5个',RespCode::PARAMS_ERR);
                return false;
            }
            $where[]=['city_id','in',$codes];
        }else{
            //不限制区域
            $p_code=db('region')->where('city_code',$params['city_code'])->value('p_code');
            $where[]=['city_ids','like', ','.$p_code.','.$params['city_code'] . '%'];
        }
        //商圈名称模糊匹配
        if (isset($params['keywords'])&&$params['keywords']){
            $keywords=trim($params['keywords']);
            $where[]=['name','like', '%'.$keywords . '%'];
        }
        //查询分页数据
        $data=db('business_area')
            ->field('id,name')
            ->page($params['page'],$pageSize)
            ->where($where)
            ->select();
        //数据总数
        $count=db('business_area')
            ->where($where)
            ->count();
        $list['count'] = $count ?? 0;//数据总条数
        $list['page'] = (int)$params['page'];//当前页码
        $list['pageSize'] = $pageSize;//每页显示条数
        $list['pageNum'] = ceil($list['count'] / $pageSize);//每页显示条数
        $list['list'] = $data ? $data : [];//每页显示数据
        return $list;
    }
    /**
     * 获取商圈名
     * @param $id
     * @return mixed
     */
    public static function get_Name($id) {
        return Db::name('business_area')->where('id', $id)->value('name');
    }

    /**
     * 市下面的全部商圈
     * @param $params
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getList($params) {
        $column = ['name', 'city_code', 'id','x(location) as lng','Y(location) as lat',];
        //区域
        $rank3 = db::name('region')
            ->where('p_code', $params['city_code'])
            ->field($column)
            ->order('weights asc')
            ->select();

        foreach ($rank3 as $k => $v){
            if (!$v['lng']||!$v['lat']){
                unset($rank3[$k]);
            }
        }
        $rank3=array_values($rank3);
        $ids = array_column($rank3, 'city_code');
        //区域下面所有商圈
        $business_area = db::name('business_area')
            ->where('city_id', 'in', $ids)
            ->where('lng', '>', 0)
            ->where('lat', '>', 0)
            ->field('name,city_id,x(location) as lng,Y(location) as lat')
            ->select();
        $rank3 = array_column($rank3, null, 'city_code');
        foreach ($business_area as $val) {
            if (isset($rank3[$val['city_id']])) {
                $rank3[$val['city_id']]['children'][] = $val;
            }
        }
        $rank3 = array_values($rank3);
        foreach ($rank3 as $k => $v) {
            unset($rank3[$k]['city_code']);
            unset($rank3[$k]['id']);
            if (!empty($rank3[$k]['children'])) {
                foreach ($rank3[$k]['children'] as $k1 => $v1) {
                    $rank3[$k]['children'][$k1]['lng'] = (string)$v['children'][$k1]['lng'];
                    $rank3[$k]['children'][$k1]['lat'] = (string)$v['children'][$k1]['lat'];
                    unset($rank3[$k]['children'][$k1]['city_id']);
                }
            } else {
                unset($rank3[$k]);
            }
        }
        return array_values($rank3);
    }

    /**
     * 市下面的区是否有商圈
     * @param $params
     * @return int
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function has_data($params) {
        $rank3 = db::name('region')
            ->where('p_code', $params['city_code'])
            ->select();
        $ids = array_column($rank3, 'city_code');
        //区域下面所有商圈
        $business_area = db::name('business_area')
            ->where('city_id', 'in', $ids)
            ->select();
        if ($business_area) {
            return 1;
        } else {
            return 2;
        }
    }

}
